15 virt-install examples   KVM virtualization commands cheatsheet

Sommaire

Pre-requisite

1. virt-install ­­­­--name

2. virt-install --vcpus

3. virt-install --memory

4. virt-install --os-type and --os-variant

5. virt-install --network

6. virt-install example with multiple networks

7. virt-install --location

8. virt-install console ttyS0 (--extra-args)

9. virt-install --noautoconsole

10. virt-install --graphics with virt-viewer

11. virt-install --graphics with VNC Viewer

12. virt-install --disk (path, size, format)

13. virt-install --autostart

14. virt-install example with multiple disks

15. virt-install --pxe and --boot network

 

In this article I will share a list of commands to create KVM Virtual machine command line using virt-install examples in RHEL/CentOS 8 Linux.

virt-install is a command line tool for creating new KVM, Xen, or Linux container guests using the libvirt hypervisor management library
The virt-install tool provides a number of options that can be passed on the command line. To see a complete list of options run the following command:

# virt-install --help

 

ALSO READ:

You can also create KVM Virtual Machines using
Cockpit Web Console GUI
Virtual Manager (Deprecated starting RHEL/CentOS 8)

 

Pre-requisite

As a pre-requisite, you must first install KVM and enable KVM Virtualization on your Linux server host before you can create KVM Virtual Machine using virt-install or any other method in RHEL/CentOS 8 Linux

 

1. virt-install ­­­­--name

To define a name of the VM use --name with virt-install command

[root@rhel-8 ~]# virt-install --name centos8-2

 

2. virt-install --vcpus

To define CPU for the VM use --vcpus with virt-install command

[root@rhel-8 ~]# virt-install --name centos8-2 --vcpus 2

 

3. virt-install --memory

To define memory for the VM use --memory with virt-install command

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240

 

4. virt-install --os-type and --os-variant

You can define OS Variant and OS Type when you create VM with virt-install command
Next use --os-variant and --os-type to define the OS Variant and OS Type respectively

To get the list of supported OS Variant types, execute below command from the terminal

# osinfo-query os

In the below virt-install example cheat sheet I have defined --os-type as Linux and --os-variant as Centos7.0

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0

 

5. virt-install --network

To check list of available networks use virsh command

[root@rhel-8 ~]# virsh net-list

 Name                 State      Autostart     Persistent

----------------------------------------------------------

 default              active     yes           yes

In the below virt-install example cheat sheet we will create KVM VM and assign this default network for our Virtual Machine

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network network=default --graphics=vnc -v

Using centos7.0 default --disk size=10

 

Starting install...

Retrieving file vmlinuz...                                                            | 6.3 MB  00:00:00

Retrieving file initrd.img...                                                         |  52 MB  00:00:00

Allocating 'centos8-2-3.qcow2'                                                        |  10 GB  00:00:00

 

6. virt-install example with multiple networks

You can use --network argument multiple times to add multiple network cards to the VM
In this virt-install example cheat sheet I am creating KVM VM with one network using bridge connection and other network using default network

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network network=default  --network bridge=nm-bridge --console pty,target_type=serial --graphics=vnc -v

HINT:

You can either use --location or --cdrom to initiate the installation. I observed one difference with --location and --cdrom, with --cdrom the VM just boots using the provided ISO while with --location the installation starts automatically

 

7. virt-install --location

[root@rhel-8 ~]# virt-install --name centos8-20 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none  -v                                  

Using rhel7.6 default --disk size=10

 

Starting install...

Retrieving file vmlinuz...                                                                   | 6.3 MB  00:00:00

Retrieving file initrd.img...                                                                |  52 MB  00:00:00

Allocating 'centos8-20.qcow2'                                                                |  10 GB  00:00:00

Connected to domain centos8-20

Escape character is ^]

Also if you try to connect to your VM using virsh or Virtual Manager, you will get the same output

[root@rhel-8 ~]# virsh console centos8-2

Connected to domain centos8-2

Escape character is ^]

error: operation failed: Active console session exists for this domain

Hence this combination is not recommended to create KVM Virtual Machine

 

8. virt-install console ttyS0 (--extra-args)

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso --network bridge=nm-bridge --graphics=none --extra-args console=ttyS0 -v

Using centos7.0 default --disk size=10

 

Starting install...

Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00

Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00

Allocating 'centos8-2.qcow2'                                                                                                                          |  10 GB  00:00:00

Connected to domain centos8-2

Escape character is ^]

[    0.000000] Initializing cgroup subsys cpuset

[    0.000000] Initializing cgroup subsys cpu

[    0.000000] Initializing cgroup subsys cpuacct

use Ctrl+5 to exit the session

To reconnect to the session use below command

[root@rhel-8 ~]# virsh console centos8-2

NOTE:

You can also use --cdrom in these commands, but just to highlight, you cannot use --cdrom with kernel arguments --extra-args and the outcome would be same so it is recommended to always use --graphics while creating VM with virt-install.

You can choose to use this command based on your requirement.

 

9. virt-install --noautoconsole

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network bridge=nm-bridge  --graphics=none --console pty,target_type=serial --noautoconsole --extra-args console=ttyS0  -v

Using centos7.0 default --disk size=10

 

Starting install...

Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00

Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00

Allocating 'centos8-2-17.qcow2'                                                                                                                       |  10 GB  00:00:00

Domain installation still in progress. You can reconnect to

the console to complete the installation process.

As expected the command exited after creating the VM. Next connect to the console using virsh

[root@rhel-8 ~]# virsh console centos8-2

 

10. virt-install --graphics with virt-viewer

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network bridge=nm-bridge  --graphics=vnc --console pty,target_type=serial -v

Starting install...

Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00

Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00

Allocating 'centos8-2-17.qcow2'                                                                                                                       |  10 GB  00:00:00

Domain creation completed.

Restarting guest.

Next virt-viewer will automatically connect to the centos8-2 console

 

11. virt-install --graphics with VNC Viewer

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network bridge=nm-bridge --graphics type=vnc --console pty,target_type=serial --noautoconsole -v

Using centos7.0 default --disk size=10

 

Starting install...

Retrieving file vmlinuz...                                                                                                                            | 6.3 MB  00:00:00

Retrieving file initrd.img...                                                                                                                         |  52 MB  00:00:00

Allocating 'centos8-2-14.qcow2'                                                                                                                       |  10 GB  00:00:00

Domain installation still in progress. You can reconnect to

the console to complete the installation process.

To get the VNC port number VNC is listening on use virsh command

[root@rhel-8 ~]# virsh vncdisplay centos8-2

127.0.0.1:3

Install tigervnc on your Linux server

# yum -y install tigervnc

Next execute vncviewer to connect to the console

# vncviewer <address>:<port>

 

# vncviewer 127.0.0.1:3

 

12. virt-install --disk (path, size, format)

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso  --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc -v

 

Starting install...

 

13. virt-install --autostart

By default the VMs are configured to stay powered off when the host server reboots
You can use --autostart to also start the VM when the host server comes up after reboot

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso  --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2  --disk path=/disks/centos8-2-1.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc --autostart -v

 

14. virt-install example with multiple disks

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --cdrom=/tmp/rhel-server-7.6-x86_64-dvd.iso  --disk path=/disks/centos8-2.qcow2,size=20,format=qcow2  --disk path=/disks/centos8-2-1.qcow2,size=20,format=qcow2 --network bridge=nm-bridge --graphics=vnc  -v

 

Starting install...

Allocating 'centos8-2.qcow2'                                                                                                                          |  20 GB  00:00:00

Allocating 'centos8-2-1.qcow2'                                                                                                                        |  20 GB  00:00:00

Verify the list of disks assigned to this VM

[root@rhel-8 ~]# ls -l /disks/

total 6808

-rw-------. 1 qemu qemu 21478375424 Apr 29 03:10 centos8-2-1.qcow2

-rw-------. 1 qemu qemu 21478375424 Apr 29 03:10 centos8-2.qcow2

 

15. virt-install --pxe and --boot network

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0 --location=/tmp/rhel-server-7.6-x86_64-dvd.iso  --network network=default --pxe --extra-args "ks=/tmp/kickstart.conf" --console pty,target_type=serial  --graphics=vnc -v

Alternatively you can also use "--boot network" to boot from PXE server

[root@rhel-8 ~]# virt-install --name centos8-2 --memory 10240 --vcpus=2 --os-type=Linux --os-variant=centos7.0   --network network=default --boot network  --console pty,target_type=serial  --graphics=vnc -v

 

I have written another article with the detailed steps to perform PXE installation on KVM Virtual Machines

 

Lastly I hope this article with virt-install examples cheat sheet to create KVM Virtual Machine on Linux was helpful. So, let me know your suggestions and feedback using the comment section.